<?php
/**
*	curl的使用
*/
namespace Rainrock\Framework\Chajian;


use Rainrock\Framework\kernel\core\Chajian;
use Rainrock\Framework\kernel\base\Web;

class curlChajian extends Chajian{
	
	protected $timeout 	 = 30;
	protected $curlerros = '';
	
	
	private function strurl($url)
	{
		$url = str_replace('&#47;', '/', $url);
		$url = str_replace(' ', '', $url);
		$url = str_replace("\n", '', $url);
		return $url;
	}
	
	public function setTimeout($time)
	{
		$this->timeout = $time;
		return $this;
	}
	
	public function getfilecont($url)
	{
		$url 	 = $this->strurl($url);
		@$result = file_get_contents($url);
		return $result;
	}
	
	public function postfilecont($url, $data=array())
	{
		$url  	= $this->strurl($url);
		$cont 	= $this->getdatastr($data);
		$len 	= strlen($cont);
		$opts 	= array(  
			'http'	=>	array(  
				'method'	=>	'POST',  
				'header' 	=>
                "Content-type: application/x-www-form-urlencoded\r\n" .
                "Content-length: $len\r\n", 
				'content' => $cont,  
			)  
		 );  
		$cxContext 	= stream_context_create($opts);  
		@$sFile 	= file_get_contents($url, false, $cxContext);  
		return $sFile;
	}
	
	private function getUserAgent()
	{
		$str  = Web::getAgent();
		$str .= ' '.PHP_SAPI.'/'.PACKAGE.' VERSION/'.VERSION.'';
        return $str;
    }
	
	/**
	*	数据处理
	*/
	private function getdatastr($data)
	{
		$cont = '';
		if(is_array($data)){
			foreach($data as $k=>$v)$cont.='&'.$k.'='.$v.'';
			if($cont!='')$cont=substr($cont,1);
		}else{
			$cont 	= $data;
		}
		return $cont;
	}
	
	/**
	*	获取内容
	*/
	public function getcurl($url, $headarr=array())
	{
		$this->curlerros = '';
		if(!function_exists('curl_init')){
			return $this->getfilecont($url);
		}
		$url= $this->strurl($url);
		$ishttps = 0;
		if(substr($url,0, 5)=='https')$ishttps=1;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->getUserAgent());
		$headarr['X-FORWARDED-FOR'] = Web::getIp();
		if($ishttps==1){
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		}
		
		//设置head
		if($headarr){
			$heads = array();
			foreach($headarr as $k=>$v)$heads[] = ''.$k.':'.$v.'';
			curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
		}
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 
		$output 			= curl_exec($ch);
		$this->curlerros 	= curl_errno($ch);
		curl_close($ch);
		return $output;
	}
	
	/**
	*	post的
	*/
	public function postcurl($url, $data=array(), $headarr=array(), $lx=0)
	{
		$this->curlerros = '';
		if(!function_exists('curl_init')){
			return $this->postfilecont($url, $data);
		}
		$url	= $this->strurl($url);
		$cont 	= $data;
		if($lx==0)$cont = $this->getdatastr($data);
		$ishttps = 0;
		if(substr($url,0, 5)=='https')$ishttps=1;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header
		curl_setopt($ch, CURLOPT_POST, 1);
		@curl_setopt($ch, CURLOPT_POSTFIELDS, $cont);
		if($ishttps==1){
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		}
		curl_setopt($ch, CURLOPT_USERAGENT, $this->getUserAgent());
		$headarr['X-FORWARDED-FOR'] = Web::getIp();
		
		//设置head
		if($headarr){
			$heads = array();
			foreach($headarr as $k=>$v)$heads[] = ''.$k.':'.$v.'';
			curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
		}
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 
		$output 		 = curl_exec($ch);
		$this->curlerros = curl_errno($ch);
		curl_close($ch);
		return $output;
	}
	
	/**
	*	获取错误信息
	*/
	public function curlError()
	{
		return $this->curlerros;
	}
	
	/**
	*	返回结果处理
	*/
	public function result($str)
	{
		if(!$str)return $this->returnerror('not back');
		if(substr($str,0,1)!='{' || !$this->contain($str, 'success'))return $this->returnerror($str);
		$barr = json_decode($str, true);
		return $barr;
	}
}